package com.imau.service.impl;

import com.alibaba.fastjson.JSON;
import com.imau.bean.Permission;
import com.imau.bean.PermissionApi;
import com.imau.bean.PermissionMenu;
import com.imau.bean.PermissionPoint;
import com.imau.common.Const.PermissionConst;
import com.imau.common.entiry.ResultCode;
import com.imau.common.exception.CommonException;
import com.imau.common.utils.IdWorker;
import com.imau.common.utils.SqlSessionFactoryUtil;
import com.imau.common.utils.StringLike;
import com.imau.mapper.PermissionApiMapper;
import com.imau.mapper.PermissionMapper;
import com.imau.mapper.PermissionMenuMapper;
import com.imau.mapper.PermissionPointMapper;
import com.imau.service.PermissionService;
import io.mybatis.mapper.example.Example;
import io.mybatis.service.AbstractService;
import org.apache.ibatis.session.SqlSession;

import java.util.*;


public class PermissionServiceImpl extends AbstractService<Permission,String,PermissionMapper> implements PermissionService {

    //定义service所用到的Mapper
    private PermissionMapper permissionMapper;
    private PermissionApiMapper permissionApiMapper;
    private PermissionMenuMapper permissionMenuMapper;
    private PermissionPointMapper permissionPointMapper;

    private SqlSession session;

    private void closeMapper() {

        if (session != null) {
            session.close();
        }
    }

    private void initMapper() {

        session = SqlSessionFactoryUtil.getInstace().openSession(true);

        permissionMapper = session.getMapper(PermissionMapper.class);

        permissionApiMapper = session.getMapper(PermissionApiMapper.class);

        permissionMenuMapper = session.getMapper(PermissionMenuMapper.class);

        permissionPointMapper = session.getMapper(PermissionPointMapper.class);
    }

    @Override
    public int savePermission(Map<String, Object> map) {


        int  row = 0;

        initMapper();

        //统一生成ID 权限表及子表保持一致
        String keyId = IdWorker.getNextId();

        map.put("id", keyId);

        //3.保存权限的关联子表；
        int type = Integer.valueOf(map.get("type").toString());

        switch (type) {
            case PermissionConst.PERMISSION_MENU:

                row+= permissionMenuMapper.insertSelective(JSON.parseObject(JSON.toJSONString(map), PermissionMenu.class));
                break;
            case PermissionConst.PERMISSION_POIN:
                row+= permissionPointMapper.insertSelective(JSON.parseObject(JSON.toJSONString(map), PermissionPoint.class));
                break;

            case PermissionConst.PERMISSION_API:
                row+= permissionApiMapper.insertSelective(JSON.parseObject(JSON.toJSONString(map), PermissionApi.class));
                break;
        }

        //2.保存权限:总表
        row+= permissionMapper.insertSelective(JSON.parseObject(JSON.toJSONString(map), Permission.class));

        closeMapper();

        return row;
    }

    @Override
    public int  updatePermission(Map<String, Object> map) {

        int  row = 0;
        initMapper();

        //3.保存权限的关联子表；
        int type = Integer.valueOf(map.get("type").toString());

        switch (type) {
            case PermissionConst.PERMISSION_MENU:

                row+= permissionMenuMapper.updateByPrimaryKey(JSON.parseObject(JSON.toJSONString(map), PermissionMenu.class));
                break;
            case PermissionConst.PERMISSION_POIN:
                row+= permissionPointMapper.updateByPrimaryKey(JSON.parseObject(JSON.toJSONString(map), PermissionPoint.class));
                break;

            case PermissionConst.PERMISSION_API:
                row+= permissionApiMapper.updateByPrimaryKey(JSON.parseObject(JSON.toJSONString(map), PermissionApi.class));
                break;
        }

        //2.保存权限:总表
        row+= permissionMapper.updateByPrimaryKey(JSON.parseObject(JSON.toJSONString(map), Permission.class));;

        closeMapper();

        return row;

    }

    @Override
    public Map<String, Object> findByPermissionId(String id) {
        Object bean = null;

        Map<String, Object> map = new HashMap<>();

        initMapper();

        Permission perm = permissionMapper.selectByPrimaryKey(id).get();

        int type = perm.getType();

        if (type == PermissionConst.PERMISSION_MENU) {

            bean = permissionMenuMapper.selectByPrimaryKey(id);

        } else if (type == PermissionConst.PERMISSION_POIN) {

            bean = permissionPointMapper.selectByPrimaryKey(id);

        } else if (type == PermissionConst.PERMISSION_API) {

            bean = permissionApiMapper.selectByPrimaryKey(id);

        } else {

            new CommonException(ResultCode.FAIL);
        }


        String s = JSON.toJSONString(perm);
        String s1 = JSON.toJSONString(bean);

        Map map1 = JSON.parseObject(s, Map.class);
        Map map2 = JSON.parseObject(s1, Map.class);

        map.putAll(map1);

        map.putAll(map2);

        closeMapper();

        return map;
    }


    @Override
    public List<Permission> findAllPermission(Permission permission)
    {
        initMapper();

        Example<Permission> example = new Example<Permission>();

        //2.2 创建 动态拼接Sql对象
        Example.Criteria<Permission> criteria = example.createCriteriaSelective();

        if(permission!=null)
        {
            criteria.andLike(Permission::getName, StringLike.strCaonstLike(permission.getName()));

            criteria.andEqualTo(Permission::getEnVisible, permission.getEnVisible());

            criteria.andEqualTo(Permission::getParentId, permission.getParentId());


            if (permission.getType()!=null && permission.getType()==0)
            {
                criteria.andIn(Permission::getType, Arrays.asList(1,2));

            }else  if(permission.getType()!=null && permission.getType()==4)
            {
                criteria.andIn(Permission::getType, Arrays.asList(1,2,3));
            }else{

                criteria.andEqualTo(Permission::getType, permission.getType());
            }




        }
        example.orderByAsc(Permission::getOrderNum);
        //2.3 查询
        List<Permission> permissions = permissionMapper.selectByExample(example);

        closeMapper();

        return permissions;
    }

    @Override
    public int  deleteByPermissionId(String id) {

        initMapper();

        //根据查询出权限对象
        Permission perm = permissionMapper.selectByPrimaryKey(id).get();

        //获取类型
        int type = perm.getType();

        //先删除权限表
        int row = permissionMapper.deleteByPrimaryKey(id);

        //类型表中删除
        if (type == PermissionConst.PERMISSION_MENU) {

            row +=  permissionMenuMapper.deleteByPrimaryKey(id);

        } else if (type == PermissionConst.PERMISSION_POIN) {

            row +=  permissionPointMapper.deleteByPrimaryKey(id);

        } else if (type == PermissionConst.PERMISSION_API) {

            row +=  permissionApiMapper.deleteByPrimaryKey(id);

        } else {

            new CommonException(ResultCode.FAIL);
        }

          return row;
    }



}
